'use strict';
/* Controllers */
app.controller('memberCtrl', ['$scope', 'apiService', 'toaster', '$cookieStore', '$rootScope', 'ngDialog', function ($scope, apiService, toaster, $cookieStore, $rootScope, ngDialog) {

    $scope.user = {
        previous: $rootScope.userObj['id'],
        company: 2,
        level: 0
    };

    $scope.noUser = {
        memberId: $rootScope.userObj['id'],
        company: 2,
        role: 1
    };

    // 注册会员
    $scope.addMember = function () {
        $scope.btn_disabled = true;

        // 对话框
        $scope.dialog = {
            title: '输入密码',
            content: '注册新会员会扣除您账户上相应的资金!<br/>请输入交易密码:'
        };
        ngDialog.openConfirm({
            template: '/tpl/dialog_pass.html',
            className: 'ngdialog-theme-plain',
            scope: $scope
        }).then(function (value) {
            if (value.statu == true) {
                $scope.user.paypwd=value.val;
                apiService.post({
                    modelName: 'member',
                    apiName: 'addMemberDB'
                }, $scope.user, function (data) {
                    $scope.btn_disabled = false;
                    if (data.code == 200) {
                        toaster.pop('success', '提示', data['msc']);
                        // 对话框
                        $scope.dialog = {
                            title: '提示',
                            content: '注册新会员成功，已扣除相应的金额!<br /> 上级会员名:' + data['data'][0]['username'] + '<br /> 新创建的非会员信息:<br/ > 用户名:' + data['data'][1]['username'] + '<br />密码:123456(默认)'
                        };
                        ngDialog.openConfirm({
                            template: '/tpl/dialog.html',
                            className: 'ngdialog-theme-plain',
                            scope: $scope
                        }).then(function (value) {

                        });


                        $scope.user = {
                            previous: $rootScope.userObj['id'],
                            company: 2,
                            level: 0
                        };
                    } else {
                        toaster.pop('error', '提示', data['msc']);
                    }
                }, function (err) {
                    $scope.btn_disabled = false;
                    toaster.pop('error', '提示', '服务器出小差咯...');
                });

            }
        });



    }

    // 注册非会员
    $scope.addNoMember = function () {
        $scope.btn_disabled = true;
        apiService.post({
            modelName: 'userinfo',
            apiName: 'addUserinfo'
        }, $scope.noUser, function (data) {
            $scope.btn_disabled = false;
            if (data.code == 200) {
                toaster.pop('success', '提示', data['msc']);
                // 对话框
                $scope.dialog = {
                    title: '提示',
                    content: '注册非会员成功!<br />新创建的非会员信息:<br/ > 用户名:' + data['data']['name']
                };
                ngDialog.openConfirm({
                    template: '/tpl/dialog.html',
                    className: 'ngdialog-theme-plain',
                    scope: $scope
                }).then(function (value) {

                });

                $scope.noUser = {
                    memberId: $rootScope.userObj['id'],
                    company: 2,
                    role: 1
                };
            } else {
                toaster.pop('error', '提示', data['msc']);
            }
        }, function (err) {
            $scope.btn_disabled = false;
            toaster.pop('error', '提示', '服务器出小差咯...');
        });
    }


}]);